###########################################
###### Replication Code for Figure 3 ######
########### January 9th, 2025 #############
###########################################
rm(list=ls())

### load packages
library(dplyr)
library(quanteda)
library(quanteda.textstats)
library(quanteda.textplots)

df <- readRDS("data/workfile.rds")

df$right_wing <- ifelse(df$lrscale > 5, 1, 0)

######## Keyness


############### Culture ##############

#### Immigration
dfmat <- corpus(df %>% filter(open_immig == 1),
                text_field = "open_q") %>% tokens() %>% 
  dfm() %>% dfm_subset(., ntoken(.) > 0)
var <- docvars(dfmat, "right_wing")
var <- ifelse(var == 1, "Right-leaning", "Left-leaning")
var <- factor(var, levels = c("Right-leaning", "Left-leaning"))
dfmat1 <- dfm_group(dfmat, groups = var)
dfmat1 <- dfm_wordstem(dfmat1)
stat <- textstat_keyness(dfmat1,
                         measure = "chi2",
                         sort = TRUE)
main_plot1 <- textplot_keyness(stat, labelsize = 4.5) + theme(legend.position = "bottom") +
  theme(text = element_text(size = 18), legend.text = element_text(size = 18)) 
main_plot1


ggsave("figures/Fig3_immigration_lrscale.pdf",
       main_plot1, height = 7, width = 8)